package com.woniuxy.hotel.room.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.hotel.room.mapper.RoomTypeMapper;
import com.woniuxy.hotel.room.pojo.Cars;
import com.woniuxy.hotel.room.pojo.Room;
import com.woniuxy.hotel.room.pojo.RoomType;
import com.woniuxy.hotel.room.service.RoomService;
import com.woniuxy.hotel.room.service.imp.RoomServiceImp;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



@Controller
@RequestMapping("/cars")
public class CarsController {

	@Autowired
	private RoomService roomServiceImp;
	
	@Autowired
	private RoomTypeMapper roomTypeMapper;
	
	@Autowired
	/*
	 * 
	 * 存入选择的房间*/
    private RedisTemplate redisTemplate;
	@RequestMapping("/savecars")
	@ResponseBody
	public String carSave(String as) {
		Subject currentUser = SecurityUtils.getSubject();
		String account = (String) currentUser.getPrincipal();
		//属性名作为键 与 前端传来的键做对接 得到值
		JSONArray array = JSONArray.fromObject(as);
		
		Double all=(double) 0;
		/*BigDecimal all=new BigDecimal("0");
		BigDecimal one=new BigDecimal("0");*/
		for (int i = 0; i < array.size(); i++) {
			JSONObject jUser = array.getJSONObject(i);
			if(jUser.getInt("roomstatus")!=1){
				return "1111失败";
			}
			else{
				//房间ID
				int roomI=jUser.getInt("roomid");
				//房间类型ID
				int roomtp=jUser.getInt("roomtypeid");
				//房间
				Room room=roomServiceImp.findRoomById(roomI);
				//房间类型
				RoomType  roomType=roomTypeMapper.selectByPrimaryKey(roomtp);
				room.setRoomprice(roomType.getRoomprice());
				Double d= roomType.getRoomprice()*room.getDiscount();
				all=d+all;
				/*Room room=roomServiceImp.findRoomById(roomI);
				one=room.getRoomprice().multiply(new BigDecimal(room.getDiscount()));
				all=all.add(one);
				System.out.println(all);*/
			}
		}

		/*Object[] obj= as.getAs();*/
		/*for (int i = 0; i < obj.length; i++) {
			System.out.println(i);
		}*/
		
		redisTemplate.opsForValue().set(account+"cart",as);
		redisTemplate.opsForValue().set("nn",all);
		/*for(int i=0;i<obj.length;i++){
			System.out.println(obj[i]+"-----------||||||||||||");		
		}*/
		/*String r =(String)obj[0];*/
		
		/*System.out.println(r.getRoomtrait()+"-----------||||||||||||");*/
		/*List<Object> object=Arrays.asList(obj);
		redisTemplate.opsForValue().set("n",object);*///存入redis中
		return "成功";
	}
	/*
	 * 
	 * 取出房间*/
	@RequestMapping("/findcars")
	@ResponseBody
	public String carShow(){
		Subject currentUser = SecurityUtils.getSubject();
		String account = (String) currentUser.getPrincipal();
		/*Collection<Room> o= (Collection<Room>) redisTemplate.opsForValue().get("n");*/
		String o=(String) redisTemplate.opsForValue().get(account+"cart");
		System.out.println("-------"+o);
		return o;
		
	}
	
	
	
	/*
	 * 
	 * 总价*/
	@RequestMapping("/findcars1")
	@ResponseBody
	public Double carShow1(int daynum){

		Subject currentUser = SecurityUtils.getSubject();
		String account = (String) currentUser.getPrincipal();
		Double bD =(Double) redisTemplate.opsForValue().get("nn");
		System.out.println("bD:"+bD);
		Double b =bD*daynum;
		System.out.println("b:"+b);
		/*BigDecimal b=bD.multiply(new BigDecimal(daynum));*/
		//nnn为计算的总价
		redisTemplate.opsForValue().set("nnn",b);
		return b;
		
		
	}
	
	
	/*
	 * 
	 * 清空*/
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(){
		/*Collection<Room> o= (Collection<Room>) redisTemplate.opsForValue().get("n");*/
		 redisTemplate.delete("n");
		System.out.println("正在执行,清空");
		return "正在执行,清空";
		
		
	}
	
}
